package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.FormDefHi;
import com.ccp.dev.form.service.FormDefHiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * BPM_FORM_DEF_HI 控制器类
 *
 * @author zhaowj
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/formDefHi")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class FormDefHiController extends SysBaseController {
    @Resource
    private FormDefHiService formDefHiService;


    /**
     * 跳转到历史记录列表页面
     *
     * @return 页面路径
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/list")
    @Action(description = "跳转到历史记录列表页面", exectype = "页面跳转日志")
    public String list(HttpServletRequest request, ModelMap map) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        map.put("formDefId", formDefId);
        return getAutoView();
    }


    /**
     * 取得历史记录分页列表
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "取得历史记录分页列表", exectype = "查询日志", detail = "取得历史记录分页列表<#if success>成功<#else>失败</#if>")
    public ResultData list(HttpServletRequest request) {
        String formDefId = RequestUtil.getString(request, "formDefId");
        QueryFilter filter = new QueryFilter(request, true);
        filter.put("formDefId", formDefId);
        List<FormDefHi> list;
        try {
            list = formDefHiService.queryList(filter);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("query.success", filter.getPageBean().getTotalCount(), list);
    }

    /**
     * 删除
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description = "删除表单历史记录", exectype = "删除日志", detail = "删除表单历史记录<#if success>成功<#else>失败</#if>")
    public ResultData del(HttpServletRequest request) {
        try {
            String[] lAryId = RequestUtil.getStringAryByStr(request, "hisId");
            formDefHiService.delByIdArray(lAryId);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception ex) {
            ex.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("delete.failure");
        }
        return getResultDataSuccess("delete.success");
    }

    /**
     * 恢复
     * 恢复记录是把当前这个表单历史记录添加到表单定义表中，版本号+1
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping("/recoveryData")
    @ResponseBody
    @Action(description = "当前这个表单历史记录添加到表单定义表中", exectype = "添加或更新日志", detail = "当前这个表单历史记录添加到表单定义表中<#if success>成功<#else>失败</#if>")
    public ResultData recoveryData(HttpServletRequest request) {
        try {
            String hisId = RequestUtil.getString(request, "hisId");
            FormDefHi formDefHi = formDefHiService.getOneById(hisId);
            formDefHiService.recoveryData(formDefHi);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("operation.success");
    }


}
